package com.zh.media.message;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zh.media.domain.dto.UpdateWorkInfoDto;
import com.zh.media.service.WorkInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class WorkInfoConsumer {
    private final ObjectMapper objectMapper;
    private final WorkInfoService workInfoService;

    @RabbitListener(
            bindings =
            @QueueBinding(
                    value = @Queue(value = "ls.media.praise.queue", durable = "true"),
                    exchange = @Exchange(value = "ls.media.exchange"),
                    key = "praise"
            )
    )
    public void praiseQueue(Message message) {
        try {
            UpdateWorkInfoDto updateWorkInfoDto = objectMapper.readValue(message.getBody(), UpdateWorkInfoDto.class);
            if (updateWorkInfoDto != null) {
                workInfoService.praise(updateWorkInfoDto);
            }
        } catch (JsonProcessingException e) {
            log.error("点赞消息转换异常", e);
        } catch (Exception e) {
            log.error("点赞消息处理异常", e);
        }
    }

    @RabbitListener(
            bindings =
            @QueueBinding(
                    value = @Queue(value = "ls.media.collect.queue", durable = "true"),
                    exchange = @Exchange(value = "ls.media.exchange"),
                    key = "collect"
            )
    )
    public void collectQueue(Message message) {
        try {
            UpdateWorkInfoDto updateWorkInfoDto = objectMapper.readValue(message.getBody(), UpdateWorkInfoDto.class);
            if (updateWorkInfoDto != null) {
                workInfoService.collect(updateWorkInfoDto);
            }
        } catch (JsonProcessingException e) {
            log.error("收藏消息转换异常", e);
        } catch (Exception e) {
            log.error("收藏消息处理异常", e);
        }
    }

    @RabbitListener(
            bindings =
            @QueueBinding(
                    value = @Queue("ls.media.view.queue"),
                    exchange = @Exchange(value = "ls.media.exchange"),
                    key = "view"
            )
    )
    public void viewQueue(Message message) {
        try {
            UpdateWorkInfoDto updateWorkInfoDto = objectMapper.readValue(message.getBody(), UpdateWorkInfoDto.class);
            if (updateWorkInfoDto != null) {
                workInfoService.updateView(updateWorkInfoDto);
            }
        } catch (JsonProcessingException e) {
            log.error("浏览量消息转换异常", e);
        } catch (Exception e) {
            log.error("浏览量消息处理异常", e);
        }
    }

    @RabbitListener(
            bindings =
            @QueueBinding(
                    value = @Queue("ls.media.share.queue"),
                    exchange = @Exchange(value = "ls.media.exchange"),
                    key = "share"
            )
    )
    public void shareQueue(Message message) {
        try {
            UpdateWorkInfoDto updateWorkInfoDto = objectMapper.readValue(message.getBody(), UpdateWorkInfoDto.class);
            if (updateWorkInfoDto != null) {
                workInfoService.updateShare(updateWorkInfoDto);
            }
        } catch (JsonProcessingException e) {
            log.error("分享消息转换异常", e);
        } catch (Exception e) {
            log.error("分享消息处理异常", e);
        }
    }
}
